package com.lanou3g.musicenjoy.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou3g.musicenjoy.R;
import com.lanou3g.musicenjoy.entity.SingerInfor;
import com.lanou3g.musicenjoy.fragment.MusicFragment;
import com.lanou3g.musicenjoy.fragment.SpecialFragment;

import com.lanou3g.musicenjoy.volley.VolleySingleton;
import com.squareup.picasso.Picasso;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by dllo on 15/10/20.
 */
public class SingerInforActivityFragment extends Fragment implements View.OnClickListener {



    @Bind(R.id.singerinforImageView)
    ImageView singerinforImageView;
    @Bind(R.id.singername)
    TextView singername;
    @Bind(R.id.singerstart)
    TextView singerstart;
    @Bind(android.R.id.tabs)
    TabWidget tabs;
    @Bind(R.id.music)
    FrameLayout music;
    @Bind(R.id.special)
    FrameLayout special;
    @Bind(android.R.id.tabcontent)
    FrameLayout tabcontent;

    private SingerInfor singerInfor;
    private ImageLoader imageLoader;
    private FragmentManager manager;
    private VolleySingleton volleySingleton;
    private MusicFragment musicFragment;
    private SpecialFragment specialFragment;
    private RequestQueue requestQueue;
    private View view;
    private ImageView singerBack;
    private TabHost tabhost;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.activity_singerinfor,null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        volleySingleton = VolleySingleton.getInstance(getActivity());
        requestQueue = VolleySingleton.getInstance(getActivity()).getRequestQueue();
        imageLoader = VolleySingleton.getInstance(getActivity()).getImageLoader();
       // setContentView(R.layout.activity_singerinfor);
        singername= (TextView) view.findViewById(R.id.singername);
        singerBack= (ImageView) view.findViewById(R.id.singerBack);
        singerBack.setOnClickListener(this);
        tabhost= (TabHost) view.findViewById(android.R.id.tabhost);
        singerstart= (TextView) view.findViewById(R.id.singerstart);
        singerinforImageView= (ImageView) view.findViewById(R.id.singerinforImageView);
        tabhost.setup();


        //歌曲
        TabHost.TabSpec music = tabhost.newTabSpec("music");
        //View view=getLayoutInflater().inflate(R.layout.music_item, null);
        View vo=View.inflate(getActivity(),R.layout.music_item,null);
        music.setIndicator(vo);
        music.setContent(R.id.music);
        tabhost.addTab(music);

        //专辑
        TabHost.TabSpec special = tabhost.newTabSpec("special");
//        View v=getLayoutInflater().inflate(R.layout.special_item,null);
        View v=View.inflate(getActivity(),R.layout.special_item,null);
        special.setIndicator(v);
        special.setContent(R.id.special);
        tabhost.addTab(special);

        manager = getActivity().getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        musicFragment = new MusicFragment();
        specialFragment = new SpecialFragment();
        transaction.replace(R.id.music, musicFragment);
        transaction.replace(R.id.special, specialFragment);
        transaction.commit();

        Bundle bundle1 = getArguments();
        String a = bundle1.getString("a");
        Log.i("fjajfj",a);
        Bundle bundle = new Bundle();
        bundle.putString("a", a);
        musicFragment.setArguments(bundle);
        specialFragment.setArguments(bundle);
        Log.i("djdjjjj", a);
        String url = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0" +
                "&method=baidu.ting.artist.getinfo&format=json&tinguid=" + a + "&artistid=" + a;
        Log.i("xinxin", url + "");
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response != null) {
                    Gson gson = new Gson();
                    singerInfor = gson.fromJson(response, SingerInfor.class);
                    if (singerInfor != null) {
                        singername.setText(singerInfor.getName());
                        singerstart.setText(singerInfor.getCountry());
                        Picasso.with(getActivity()).load(singerInfor.getAvatar_s1000()).into(singerinforImageView);

                        Log.i("hfka", singerInfor.getName());
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        requestQueue.add(request);

        ButterKnife.bind(getActivity());
    }




    @Override
    public void onClick(View v) {
   getFragmentManager().popBackStack();
    }
}
